package org.weasel.business;

import java.util.ArrayList;

import org.weasel.data.SettingsManager;

public class Settings {
	private String companyName = "";
	private ArrayList<Project> projects = new ArrayList<Project>();
	private ArrayList<Developer> developers = new ArrayList<Developer>();
	private SettingsManager settingsMan;
	
	/***
	 * This is the constructor of the Settings object. It takes a path of an XML file
	 * as parameter and if the file already exists it loads all the data contained in that
	 * file. Otherwise an empty xml file with the base structure is created.
	 * The structure looks like this:
	 * <settings>
	 * 		<companyName>name</companyNameY
	 * 		<developers>
	 * 		</developers>
	 * 		<projects>
	 * 		</projects>
	 * </settings>
	 * @param path
	 * @throws Exception
	 */
	public Settings(String path) throws Exception {
		settingsMan = new SettingsManager(path);
		if(settingsMan.getFileExists()) {
			companyName = settingsMan.getCompanyName();
			developers = settingsMan.getDevelopers();
			projects = settingsMan.getProjects();
		}
	}

	/***
	 * This method returns the company name of the actual settings object
	 * @return
	 */
	public String getCompanyName() {
		return companyName;
	}

	/***
	 * This method sets the company name of the actual settings object
	 * @param companyName
	 * @throws Exception
	 */
	public void setCompanyName(String companyName) throws Exception {
		settingsMan.setCompanyName(companyName);
		this.companyName = companyName;
	}
	
	public void setuserStory(String id) throws Exception{
		settingsMan.setuserStory(id);
	}
	
	public String getuserStory(){
		try {
			return settingsMan.getuserStory();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * This method returns an arraylist of all the developers of the company
	 * @return
	 */
	public ArrayList<Developer> getDevelopers() {
		return developers;
	}
	
	/***
	 * This method returns an arraylist of all the projects of the company
	 * @return
	 */
	public ArrayList<Project> getProjects() {
		return projects;
	}
	

	
	/***
	 * Creates a new Project, adds it to the list projects and stores it in the xml file
	 * and returns the created project.
	 * @param name
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public Project createAndAddProject(String name, String path) throws Exception {
		String id = Utilities.createUniqueId();
		Project project = new Project(id, name, path, this.developers);
		settingsMan.storeProject(project);
		projects.add(project);
		return project;
	}

	/***
	 * Removes a Project from the projects and the xml file and deletes the
	 * associated project xml file
	 * @param projectToDelete
	 * @throws Exception
	 */
	public void removeProject(Project projectToDelete) throws Exception {
		projects.remove(projectToDelete);
		settingsMan.deleteProject(projectToDelete);
		Utilities.deleteFile(projectToDelete.getPath());
	}

	/***
	 * Adds a developer to the list of developers and stores it in the xml file
	 * @param developerToAdd
	 * @throws Exception
	 */
	public void addDeveloper(Developer developerToAdd) throws Exception {
		settingsMan.storeDeveloper(developerToAdd);
		developers.add(developerToAdd);
	}
	
	/***
	 * Creates a new Developer and adds it to the list of developers, stores it in
	 * the xml file and returns the created developer.
	 * @param name
	 * @param mail
	 * @param position
	 * @return
	 * @throws Exception
	 */
	public Developer createAndAddDeveloper(String name, String mail, String position) throws Exception {
		String uniqueID = Utilities.createUniqueId();
		Developer developer = new Developer(uniqueID, name, mail, position);
		addDeveloper(developer);
		return developer;
	}
	
	/***
	 * Removes a developer from the list of developers and deletes it from
	 * the xml file.
	 * @param developerToRemove
	 * @throws Exception
	 */
	public void removeDeveloper(Developer developerToRemove) throws Exception {
		settingsMan.deleteDeveloper(developerToRemove);
		this.developers.remove(developerToRemove);
	}

	public Developer getDeveloperByID(String developerID) {
		for (Developer developer : developers) {
			if(developer.getId().equals(developerID))
				return developer;
		}
		return null;
	}
}
